﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Data;
using System.Collections;

using CE.Database;
using CE.Global;
using CE.Configuration;

public partial class home : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    string selectedLanguage;
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.Page.Title = Resources.Title.HomeTitle;

        LiteralControl newln = new LiteralControl(Environment.NewLine);
        HtmlMeta language = new HtmlMeta();
        language.Name = "language";
        language.Content = Resources.Title.PageLanguage;

        HtmlMeta keywords = new HtmlMeta();
        keywords.Name = "keywords";
        keywords.Content = Resources.Title.HomeKeyword;

        HtmlMeta description = new HtmlMeta();
        description.Name = "description";
        description.Content = Resources.Title.HomeDescription;

        Page.Header.Controls.AddAt(10, new LiteralControl(Environment.NewLine));
        Page.Header.Controls.AddAt(11, language);
        Page.Header.Controls.AddAt(12, new LiteralControl(Environment.NewLine));
        Page.Header.Controls.AddAt(13, keywords);
        Page.Header.Controls.AddAt(14, new LiteralControl(Environment.NewLine));
        Page.Header.Controls.AddAt(15, description);
        if (!IsPostBack)
        {
            #region Promotion Display
            string _CNYPromo = CE.Global.General.GetUTCDateTime().ToString("yyyyMMdd");
            Int64 _intCNYPromo = 0;
            if (!Int64.TryParse(_CNYPromo, out _intCNYPromo))
            {
                _intCNYPromo = 0;
            }
            if ((_intCNYPromo >= 20150202 && _intCNYPromo <= 20150227) || (_intCNYPromo >= 25580202 && _intCNYPromo <= 25580227))
            {
                divPromo.Visible = true;
                hdnIsPromo.Value = "1";
                hplPromoCarQuote.NavigateUrl = "car-insurance/car-quote.aspx";
            }
            #endregion
            LoadTitles();
        }
    }

    protected void btnGetQuote_Click(object sender, EventArgs e)
    {
        try
        {
            //string _strCarRedirectURL = "car-insurance/car-quote.aspx?type=hmm";
            string _strCarRedirectURL = GetCarQuoteUrl();
            Response.Redirect(_strCarRedirectURL);
        }
        catch (Exception ex)
        {

        }
    }

    private void LoadTitles()
    {
        lblTitle.Text = Resources.CarMenu.ThaiCarInsurance;
        lblCompareService.Text = Resources.CarMenu.CompareToday;
        //lblGetQuote.Text = Resources.CarMenu.GetaQuote;
        btnGetQuote.Text = Resources.CarMenu.GetaQuote;
        lblLowPremium.Text = Resources.CarMenu.LowestPrices;
        lblInsurer.Text = Resources.CarMenu.MotorInsurer;
        lblMoneyBack.Text = Resources.CarMenu.MoneyBack;
        //ibtnLanguage.ImageUrl = Resources.CarMenu.ButtonLanguageUrl;
        //imgOIC.ImageUrl = Resources.CarMenu.OICUrl;
        //imgDBD.ImageUrl = Resources.CarMenu.DBDUrl;
    }

    public string GetCarQuoteUrl()
    {
        #region Promotion Status
        bool _IsPromo = false;
        string _Promo = CE.Global.General.GetUTCDateTime().ToString("yyyyMMdd");
        Int64 _intPromo = 0;
        if (!Int64.TryParse(_Promo, out _intPromo))
        {
            _intPromo = 0;
        }
        //if (Session["Language"] != null & Session["Language"].ToString() == "en-US")
        //{
        if ((_intPromo >= 20150413 && _intPromo <= 20150430) || (_intPromo >= 25580413 && _intPromo <= 25580430))
        {
            _IsPromo = true;
        }
        //}
        //else if (Session["Language"] != null & Session["Language"].ToString() == "th-TH")
        //{
        //    if (_intPromo >= 25580413 && _intPromo <= 25580430)
        //    {
        //        _IsPromo = true;
        //    }
        //}
        #endregion

        string _url = "";
        Int32 _result = 0;
        string[] urls = new string[2];
        urls[0] = "car-insurance/promo-car-quote.aspx?type=hmmp";
        urls[1] = "car-insurance/car-quote.aspx?type=hmm";
        if (_IsPromo)
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable();
            string _spName = "sprocSelectRandomNumberFromCarQuote";

            _dtResult = _db.GetDataTableBySP(_spName);
            if (_dtResult != null && _dtResult.Rows.Count > 0)
            {
                if (!Int32.TryParse(_dtResult.Rows[0]["RandomNumber"].ToString(), out _result))
                {
                    _result = 0;
                }
            }
            _url = urls[_result];
        }
        else
        {
            _url = urls[1];
        }
        return _url;
    }

}
